import { defineStore } from "pinia";
import { useRequest } from "@/hooks";
import { APIS } from "@/service";

type AccountStoreUserType = {
    name: string;
    avatar: string;
    expert: boolean;
    phone: string;
    [k: string]: string | boolean;
};

/**
 * @description 账户状态管理类型定义
 * */
type AccountStoreType = {
    /**
     * @description 用户认证token
     * */
    token: string;

    /**
     * @description 用户信息
     * */
    user: AccountStoreUserType;
};

/**
 * @description 账户状态管理store
 * @returns {Object} 返回账户状态管理对象
 * */
export const useAccountStore = defineStore("account", {
    /**
     * @description 初始化状态
     * @returns {AccountStoreType} 返回初始状态对象
     * */
    state: (): AccountStoreType => ({
        /**
         * @description 用户token，初始为空字符串
         * */
        token: "",

        /**
         * @description 获取用户信息
         * @returns {Object} 返回用户信息对象
         * Maria Sandra Wilson
         * https://img2.baidu.com/it/u=2988927163,1019881354&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500
         * */
        user: {
            name: "微信用户",
            avatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
            expert: false,
            phone: ""
        }
    }),
    /**
     * @description 状态修改方法
     * */
    actions: {
        /**
         * @description 更新用户token
         * @param {string} payload - 新的token值
         * */
        async onUpdateAccountToken(payload: string) {
            this.token = payload;
        },

        /**
         * @description 删除用户信息
         * */
        async onDeleteUserInfo() {
            this.user = {
                name: "微信用户",
                expert: false,
                phone: "",
                avatar: "https://img2.baidu.com/it/u=2988927163,1019881354&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
            };
        },

        /**
         * @description 用户信息
         * */
        async onUpdateUserInfo() {
            /**
             * @description 用户详情请求实例
             * */
            await useRequest(APIS.USER_INFO, "GET", {
                retryCount: 3,
                retryDelay: 5000,
                cacheTime: 500,
                onSuccess: ({ data }) => {
                    console.log("data", data);
                    this.user = {
                        name: "微信用户",
                        avatar: "https://img2.baidu.com/it/u=2988927163,1019881354&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
                        expert: true,
                        phone: ""
                    };
                },
                onError: (error) => {
                    uni.showToast({
                        icon: "none",
                        title: error.errMsg
                    });
                }
            }).run();
        },

        /**
         * @description 退出登录
         * */
        async onLogout() {
            this.onUpdateAccountToken("");

            this.onDeleteUserInfo();
        }
    },
    /**
     * @description 状态计算属性
     * */
    getters: {
        /**
         * @description 判断用户是否已登录
         * @param {AccountStoreType} state - 当前状态
         * @returns {boolean} 返回登录状态，true表示已登录，false表示未登录
         * */
        isLogin: (state) => !!state.token,

        /**
         * @description 获取账户token
         * @param {AccountStoreType} state - 当前状态
         * @returns {string} 返回当前token值
         * */
        getAccountToken: (state) => state.token
    },
    /**
     * @description 持久化配置
     * */
    persist: {
        /**
         * @description 是否启用持久化
         * */
        enabled: true,
        /**
         * @description 持久化策略配置
         * */
        strategies: [
            {
                /**
                 * @description 持久化key，根据环境模式生成不同key
                 * */
                key: import.meta.env.MODE + "-account-store"
            }
        ]
    }
});
